DACSS 604 Project

Author

Rubi Gonzalez

Loading and Cleaning data

# necessary libraries

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(haven)
library(dplyr)
library(ggplot2)
library(pollster)
library(knitr)
library(corrplot)
corrplot 0.92 loaded
library(Hmisc)

Attaching package: 'Hmisc'

The following objects are masked from 'package:dplyr':

    src, summarize

The following objects are masked from 'package:base':

    format.pval, units
library(nnet)
library(stargazer)

Please cite as: 

 Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
 R package version 5.2.3. https://CRAN.R-project.org/package=stargazer 
library(MASS)

Attaching package: 'MASS'

The following object is masked from 'package:dplyr':

    select
library(broom)
library(plotly)

Attaching package: 'plotly'

The following object is masked from 'package:MASS':

    select

The following object is masked from 'package:Hmisc':

    subplot

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout
library(dotwhisker)
Warning: package 'dotwhisker' was built under R version 4.3.3
# uploading data
ccamData <- read_sav("CCAMData2023.sav")
# creating my smaller dataset!
myData <- ccamData %>%
  dplyr::select(wave,  happening, cause_recoded, worry, harm_personally, harm_US, harm_future_gen, harm_plants_animals,
                when_harm_US, generation, educ_category) %>% 
  rename("education" = "educ_category") 

# subsetting with data for the last available wave, Oct 2023
myData <- subset(myData, wave == 29)
# making variables numeric or factors
myData$wave <- as.numeric(myData$wave)
myData$happening <- as.factor(myData$happening)
myData$cause_recoded <- as.factor(myData$cause_recoded)
myData$generation <- as.factor(myData$generation)
myData$education <- as.factor(myData$education)
myData$worry <- as.numeric(myData$worry)
myData$harm_personally <- as.factor(myData$harm_personally)
myData$harm_US <- as.factor(myData$harm_US)
myData$harm_future_gen <- as.factor(myData$harm_future_gen)
myData$harm_plants_animals <- as.factor(myData$harm_plants_animals)
myData$when_harm_US <- as.factor(myData$when_harm_US)
# recoding variables
myData <- myData %>%
  mutate(wave = recode(wave, "29" = "Oct 2023"),
         
         cause_recoded = recode(cause_recoded, "3" = "CC_not_happening", "4" = "Nat_changes", "5" = "Human_act_and_nat_changes",
                                "6" = "human_act"),
         
         happening = recode(happening, "1" = "No", "3" = "Yes"),
         
         harm_personally = recode(harm_personally, "1" = "Not at all", "2" = "Only a little", "3" = "A moderate amount", 
                                  "4" = "A great deal"),
         
         harm_US = recode(harm_US, "1" = "Not at all", "2" = "Only a little", "3" = "A moderate amount", "4" = "A great deal"),
         
         harm_future_gen = recode(harm_future_gen, "1" = "Not at all", "2" = "Only a little", "3" = "A moderate amount", 
                                  "4" = "A great deal"),
         
         harm_plants_animals = recode(harm_plants_animals, "1" = "Not at all", "2" = "Only a little", "3" = "A moderate amount",
                                      "4" = "A great deal"),
         
         when_harm_US = recode(when_harm_US, "1" = "Never", "2" = "one_hun_years", "3" = "fifty_years", "4" = "twn_five_years",
                               "5" = "ten_years", "6" = "right_now"),

         
         generation = recode(generation, "1" = "GenZ", "2" = "Millennials", "3" = "GenerationX", "4" = "BabyBoomers", 
                             "5" = "Silent", "6" = "Greatest"),
         
         education = recode(education, "1" = "lessHighSchool", "2" = "highSchool", "3" = "someCollege", 
                            "4" = "bachelorDegreeUp"))
# getting rid of data where people refused to answer (-1) or said they "don't know" (0)

myData = filter(myData, worry != "-1")

myData = filter(myData, !(cause_recoded %in% c("-1", "1", "2")))
myData$cause_recoded <- droplevels(myData$cause_recoded)


myData = filter(myData, !(happening %in% c("-1", "2"))) # 2 is "don't know"
myData$happening <- droplevels(myData$happening)

myData = filter(myData, !(harm_personally %in% c("-1", "0")))
myData$harm_personally <- droplevels(myData$harm_personally)

myData = filter(myData, !(harm_US %in% c("-1", "0")))
myData$harm_US <- droplevels(myData$harm_US)

myData = filter(myData, !(harm_future_gen %in% c("-1", "0")))
myData$harm_future_gen <- droplevels(myData$harm_future_gen)

myData = filter(myData, !(harm_plants_animals %in% c("-1", "0")))
myData$harm_plants_animals <- droplevels(myData$harm_plants_animals)

myData = filter(myData, generation != "Greatest")
myData$generation <- droplevels(myData$generation)

myData = filter(myData, when_harm_US != "-1")
myData$when_harm_US <- droplevels(myData$when_harm_US)
# checking dataset
head(myData, 20)
# A tibble: 20 × 11
   wave    happening cause_recoded worry harm_personally harm_US harm_future_gen
   <chr>   <fct>     <fct>         <dbl> <fct>           <fct>   <fct>          
 1 Oct 20… Yes       human_act         4 A great deal    A grea… A great deal   
 2 Oct 20… Yes       Nat_changes       3 A moderate amo… A grea… A great deal   
 3 Oct 20… Yes       human_act         3 A great deal    A grea… A great deal   
 4 Oct 20… Yes       Nat_changes       2 A moderate amo… A grea… A moderate amo…
 5 Oct 20… No        Nat_changes       1 Not at all      Not at… Not at all     
 6 Oct 20… No        CC_not_happe…     1 Not at all      Not at… Not at all     
 7 Oct 20… No        Nat_changes       1 Not at all      A mode… A moderate amo…
 8 Oct 20… Yes       human_act         4 A moderate amo… A mode… A moderate amo…
 9 Oct 20… Yes       human_act         3 A moderate amo… A mode… A great deal   
10 Oct 20… Yes       human_act         3 A great deal    A grea… A great deal   
11 Oct 20… Yes       human_act         3 A great deal    A grea… A great deal   
12 Oct 20… Yes       Nat_changes       4 A great deal    A grea… A great deal   
13 Oct 20… Yes       Nat_changes       3 Only a little   Only a… A moderate amo…
14 Oct 20… Yes       human_act         3 A moderate amo… A mode… A great deal   
15 Oct 20… Yes       human_act         3 A great deal    A grea… A great deal   
16 Oct 20… Yes       Nat_changes       3 A moderate amo… A mode… A great deal   
17 Oct 20… Yes       Nat_changes       3 A moderate amo… A mode… A moderate amo…
18 Oct 20… No        human_act         2 Only a little   A mode… A moderate amo…
19 Oct 20… Yes       Nat_changes       3 A great deal    A grea… A great deal   
20 Oct 20… Yes       Human_act_an…     2 Not at all      Not at… A moderate amo…
# ℹ 4 more variables: harm_plants_animals <fct>, when_harm_US <fct>,
#   generation <fct>, education <fct>
# creating the reference variable now
myData$happening <- relevel(myData$happening, ref = "No")
myData$education <- relevel(myData$education, ref = "lessHighSchool")
myData$cause_recoded <- relevel(myData$cause_recoded, ref = "Human_act_and_nat_changes")
myData$harm_personally <- relevel(myData$harm_personally, ref = "Not at all")
myData$harm_US <- relevel(myData$harm_US, ref = "Not at all")
myData$harm_future_gen <- relevel(myData$harm_future_gen, ref = "Not at all")
myData$harm_plants_animals <- relevel(myData$harm_plants_animals, ref = "Not at all")
myData$when_harm_US <- relevel(myData$when_harm_US, ref = "Never")

Creating Subsets of the Data (there are two)

# only getting people who believe climate change is happening

sub_hap <- myData

sub_hap <- filter(sub_hap, happening != "No")
# only getting people who believe climate change is happening AND its caused by human activities in some capacity

sub_cause_hap <- sub_hap

sub_cause_hap <- filter(sub_cause_hap, !(cause_recoded %in% c("CC_not_happening", "Nat_changes")))

Visualization of Data

% of Worry

graph_myD <- myData %>%
  group_by(worry) %>%
  summarise(count = n(),
            percentage = round((n() / nrow(myData)) * 100), 2) %>% 
  mutate(worry = recode(worry,
                        "1" = "Not at all worried",
                        "2" = "Not very worried",
                        "3" = "Somewhat worried",
                        "4" = "Very worried"))

p1 <- plot_ly(data = graph_myD, x = ~ worry,
              y = ~ percentage,
              text = ~ paste(percentage, "%"),
              textposition = "inside",
              hovertext = ~paste(worry, "\n", "Percentage = ", percentage, "%"),
              hoverinfo = "text",
              marker = list(color = c("#C6DBEF", "#6BAED6", "#2171B5", "#08306B"))) %>% 
        add_bars() %>% 
        layout(title = "Worry of Climate Change",
                xaxis = list(title = "Worry"),
                yaxis = list(title = "Percentage"))

p1

Harm types

harms <- myData %>% 
  group_by(harm_personally, harm_US, harm_future_gen, harm_plants_animals)

harm_longer <- pivot_longer(harms,
                            cols = starts_with("harm_"),
                            names_to = "harm_type",
                            values_to = "response") %>% 
count(harm_type, response)

response_counts <- harm_longer %>% 
  group_by(response) %>% 
  summarise(total = sum(n))

harm_longer <- harm_longer %>% 
  left_join(response_counts, by = "response") %>% 
  mutate(percentage = round((n / total) * 100), 2)

harm_longer$response <- factor(harm_longer$response,
                               levels = c("Not at all", "Only a little", "A moderate amount", "A great deal"))
p2 <- plot_ly(harm_longer, x = ~response, y = ~percentage, color = ~harm_type, type = "bar",
              barmode = "group", 
              text = ~ paste(percentage, "%"), 
              textposition = "outside",
              hovertext = ~paste(harm_type, "\n", "Percentage = ", percentage, "%"),
              hoverinfo = "text",
              colors = RColorBrewer::brewer.pal(4, "Dark2")) %>%
  layout(title = "Percentage of Preceived Harm from Climate Change",
                xaxis = list(title = "Harm Level"),
                yaxis = list(title = "Percentage"))

p2
Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

Worry by Recorded Cause (an example for worry by some variable)

wHappening <- myData %>%
  group_by(happening) %>% 
  mutate(happening_count = n()) %>% 
  group_by(happening, worry) %>% 
  mutate(worry = recode(worry,
                        "1" = "Not at all worried",
                        "2" = "Not very worried",
                        "3" = "Somewhat worried",
                        "4" = "Very worried")) %>% 
  summarise(count = n(),
            percentage = round((count / first(happening_count)) * 100)) %>% 
  ungroup()
`summarise()` has grouped output by 'happening'. You can override using the
`.groups` argument.
#wCause[nrow(wCause) + 1,] <- list("Climate Change Not Happening", "Very worried", 0, 0)

wHappening <- data.frame(wHappening)
wHappening$happening <- factor(wHappening$happening,
                   levels = c("No", "Yes"))

wHappening$worry <- factor(wHappening$worry,
                   levels = c("Not at all worried", "Not very worried", "Somewhat worried", "Very worried"))


g <- ggplot(wHappening, aes(x = happening, y = percentage)) +
  geom_bar(stat = "identity", fill = "#AE445A") +
  labs(title = "Worry of Climate Change by Belief of Climate Change Happening", x = "Happening", y = "Percentage") +
  theme_bw() +
  facet_wrap(~worry) +
  coord_flip()

p <- ggplotly(g)

p
wCause <- myData %>%
  group_by(cause_recoded) %>% 
  mutate(cause_recoded = recode(cause_recoded, 
                                "CC_not_happening" = "Climate Change Not Happening",
                                "Nat_changes" = "Natural Change",
                                "Human_act_and_nat_changes" = "Human Activities and Natural Change",
                                "human_act" = "Human Activities")) %>% 
  mutate(cause_count = n()) %>% 
  group_by(cause_recoded, worry) %>% 
  mutate(worry = recode(worry,
                        "1" = "Not at all worried",
                        "2" = "Not very worried",
                        "3" = "Somewhat worried",
                        "4" = "Very worried")) %>% 
  summarise(count = n(),
            percentage = round((count / first(cause_count)) * 100)) %>% 
  ungroup()
`summarise()` has grouped output by 'cause_recoded'. You can override using the
`.groups` argument.
wCause[nrow(wCause) + 1,] <- list("Climate Change Not Happening", "Very worried", 0, 0)

wCause <- data.frame(wCause)

wCause
                         cause_recoded              worry count percentage
1  Human Activities and Natural Change Not at all worried     1          2
2  Human Activities and Natural Change   Not very worried    10         22
3  Human Activities and Natural Change   Somewhat worried    18         39
4  Human Activities and Natural Change       Very worried    17         37
5         Climate Change Not Happening Not at all worried    34         85
6         Climate Change Not Happening   Not very worried     5         12
7         Climate Change Not Happening   Somewhat worried     1          2
8                       Natural Change Not at all worried    72         33
9                       Natural Change   Not very worried    70         32
10                      Natural Change   Somewhat worried    63         29
11                      Natural Change       Very worried    13          6
12                    Human Activities Not at all worried     9          2
13                    Human Activities   Not very worried    33          6
14                    Human Activities   Somewhat worried   218         42
15                    Human Activities       Very worried   254         49
16        Climate Change Not Happening       Very worried     0          0
wCause$cause_recoded <- factor(wCause$cause_recoded,
                   levels = c("Climate Change Not Happening", "Natural Change", "Human Activities and Natural Change",
                              "Human Activities"))

wCause$worry <- factor(wCause$worry,
                   levels = c("Not at all worried", "Not very worried", "Somewhat worried", "Very worried"))

wCause <- dplyr::filter(wCause, worry %in% c("Somewhat worried", "Very worried"))

g <- ggplot(wCause, aes(x = cause_recoded, y = percentage)) +
  geom_bar(stat = "identity", fill = "#023C5D") +
  labs(title = "Worry of Climate Change by Recorded Cause", x = "Recorded Cause", y = "Percentage") +
  theme_bw() +
  facet_wrap(~worry) +
  coord_flip()

p <- ggplotly(g)

p
wPersonally <- myData %>%
  group_by(harm_personally) %>% 
  mutate(per_count = n()) %>% 
  group_by(harm_personally, worry) %>% 
  mutate(worry = recode(worry,
                        "1" = "Not at all worried",
                        "2" = "Not very worried",
                        "3" = "Somewhat worried",
                        "4" = "Very worried")) %>% 
  summarise(count = n(),
            percentage = round((count / first(per_count)) * 100)) %>% 
  ungroup()
`summarise()` has grouped output by 'harm_personally'. You can override using
the `.groups` argument.
#wCause[nrow(wCause) + 1,] <- list("Climate Change Not Happening", "Very worried", 0, 0)

wPersonally <- data.frame(wPersonally)
wPersonally$harm_personally <- factor(wPersonally$harm_personally,
                   levels = c("Not at all", "Only a little", "A moderate amount", "A great deal"))

wPersonally$worry <- factor(wPersonally$worry,
                   levels = c("Not at all worried", "Not very worried", "Somewhat worried", "Very worried"))


g <- ggplot(wPersonally, aes(x = harm_personally, y = percentage)) +
  geom_bar(stat = "identity", fill = "#3B1E54") +
  labs(title = "Worry of Climate Change by Belief of Personal Harm", x = "Harm Level", y = "Percentage") +
  theme_bw() +
  facet_wrap(~worry) +
  coord_flip()

p <- ggplotly(g)

p
wUS <- myData %>%
  group_by(harm_US) %>% 
  mutate(US_count = n()) %>% 
  group_by(harm_US, worry) %>% 
  mutate(worry = recode(worry,
                        "1" = "Not at all worried",
                        "2" = "Not very worried",
                        "3" = "Somewhat worried",
                        "4" = "Very worried")) %>% 
  summarise(count = n(),
            percentage = round((count / first(US_count)) * 100)) %>% 
  ungroup()
`summarise()` has grouped output by 'harm_US'. You can override using the
`.groups` argument.
#wCause[nrow(wCause) + 1,] <- list("Climate Change Not Happening", "Very worried", 0, 0)

wUS <- data.frame(wUS)
wUS$harm_US <- factor(wUS$harm_US,
                   levels = c("Not at all", "Only a little", "A moderate amount", "A great deal"))

wUS$worry <- factor(wUS$worry,
                   levels = c("Not at all worried", "Not very worried", "Somewhat worried", "Very worried"))


g <- ggplot(wUS, aes(x = harm_US, y = percentage)) +
  geom_bar(stat = "identity", fill = "#FF2929") +
  labs(title = "Worry of Climate Change by Belief of Harm to US", x = "Harm Level", y = "Percentage") +
  theme_bw() +
  facet_wrap(~worry) +
  coord_flip()

p <- ggplotly(g)

p
wFuture <- myData %>%
  group_by(harm_future_gen) %>% 
  mutate(future_count = n()) %>% 
  group_by(harm_future_gen, worry) %>% 
  mutate(worry = recode(worry,
                        "1" = "Not at all worried",
                        "2" = "Not very worried",
                        "3" = "Somewhat worried",
                        "4" = "Very worried")) %>% 
  summarise(count = n(),
            percentage = round((count / first(future_count)) * 100)) %>% 
  ungroup()
`summarise()` has grouped output by 'harm_future_gen'. You can override using
the `.groups` argument.
#wCause[nrow(wCause) + 1,] <- list("Climate Change Not Happening", "Very worried", 0, 0)

wFuture <- data.frame(wFuture)
wFuture$harm_future_gen <- factor(wFuture$harm_future_gen,
                   levels = c("Not at all", "Only a little", "A moderate amount", "A great deal"))

wFuture$worry <- factor(wFuture$worry,
                   levels = c("Not at all worried", "Not very worried", "Somewhat worried", "Very worried"))


g <- ggplot(wFuture, aes(x = harm_future_gen, y = percentage)) +
  geom_bar(stat = "identity", fill = "#FAB12F") +
  labs(title = "Worry of Climate Change by Belief of Harm to Future Gens", x = "Harm Level", y = "Percentage") +
  theme_bw() +
  facet_wrap(~worry) +
  coord_flip()

p <- ggplotly(g)

p
wPA <- myData %>%
  group_by(harm_plants_animals) %>% 
  mutate(PA_count = n()) %>% 
  group_by(harm_plants_animals, worry) %>% 
  mutate(worry = recode(worry,
                        "1" = "Not at all worried",
                        "2" = "Not very worried",
                        "3" = "Somewhat worried",
                        "4" = "Very worried")) %>% 
  summarise(count = n(),
            percentage = round((count / first(PA_count)) * 100)) %>% 
  ungroup()
`summarise()` has grouped output by 'harm_plants_animals'. You can override
using the `.groups` argument.
#wCause[nrow(wCause) + 1,] <- list("Climate Change Not Happening", "Very worried", 0, 0)

wPA <- data.frame(wPA)
wPA$harm_plants_animals <- factor(wPA$harm_plants_animals,
                   levels = c("Not at all", "Only a little", "A moderate amount", "A great deal"))

wPA$worry <- factor(wPA$worry,
                   levels = c("Not at all worried", "Not very worried", "Somewhat worried", "Very worried"))


g <- ggplot(wPA, aes(x = harm_plants_animals, y = percentage)) +
  geom_bar(stat = "identity", fill = "#54473F") +
  labs(title = "Worry of Climate Change by Belief of Harm to Plants & Animals", x = "Harm Level",
       y = "Percentage") +
  theme_bw() +
  facet_wrap(~worry) +
  coord_flip()

p <- ggplotly(g)

p
wWhen <- myData %>%
  group_by(when_harm_US) %>% 
  mutate(when_count = n()) %>% 
  group_by(when_harm_US, worry) %>% 
  mutate(worry = recode(worry,
                        "1" = "Not at all worried",
                        "2" = "Not very worried",
                        "3" = "Somewhat worried",
                        "4" = "Very worried")) %>% 
  summarise(count = n(),
            percentage = round((count / first(when_count)) * 100)) %>% 
  ungroup()
`summarise()` has grouped output by 'when_harm_US'. You can override using the
`.groups` argument.
wWhen <- data.frame(wWhen)

wWhen
     when_harm_US              worry count percentage
1           Never Not at all worried    88         73
2           Never   Not very worried    30         25
3           Never   Somewhat worried     2          2
4   one_hun_years Not at all worried    17         33
5   one_hun_years   Not very worried    18         35
6   one_hun_years   Somewhat worried    14         27
7   one_hun_years       Very worried     2          4
8     fifty_years Not at all worried     6         10
9     fifty_years   Not very worried    29         48
10    fifty_years   Somewhat worried    23         38
11    fifty_years       Very worried     3          5
12 twn_five_years Not at all worried     3          4
13 twn_five_years   Not very worried    17         22
14 twn_five_years   Somewhat worried    50         66
15 twn_five_years       Very worried     6          8
16      ten_years   Not very worried    14         19
17      ten_years   Somewhat worried    39         53
18      ten_years       Very worried    20         27
19      right_now Not at all worried     2          0
20      right_now   Not very worried    10          2
21      right_now   Somewhat worried   172         39
22      right_now       Very worried   253         58
wWhen$when_harm_US <- factor(wWhen$when_harm_US,
                   levels = c("Never", "one_hun_years", "fifty_years", "twn_five_years", "ten_years", "right_now"))

wWhen <- wWhen %>% 
  mutate(when_harm_US = recode(when_harm_US, "one_hun_years" = "100 years", "fifty_years" = "50 years", "twn_five_years" = "25 years",
                               "ten_years" = "10 years", "right_now" = "Now"))

wWhen$worry <- factor(wWhen$worry,
                   levels = c("Not at all worried", "Not very worried", "Somewhat worried", "Very worried"))


g <- ggplot(wWhen, aes(x = when_harm_US, y = percentage)) +
  geom_bar(stat = "identity", fill = "#FF885B") +
  labs(title = "Worry of Climate Change by When it will Harm the US", x = "When it will Harm the US", y = "Percentage") +
  theme_bw() +
  facet_wrap(~worry) +
  coord_flip()

p <- ggplotly(g)

p

Exploring different types of heat maps (there are 3)

heat_data <- sub_cause_hap %>% 
  mutate(cause_recoded = recode(cause_recoded, "CC_not_happening" = 1, "Nat_changes" = 2, "Human_act_and_nat_changes" = 3,
                                "human_act" = 4),
         
         happening = recode(happening, "No" = 1, "Yes" = 2),
         
         harm_personally = recode(harm_personally, "Not at all" = 1, "Only a little" = 2, "A moderate amount" = 3,
                                  "A great deal" = 4),
         
         harm_US = recode(harm_US, "Not at all" = 1, "Only a little" = 2, "A moderate amount" = 3, "A great deal" = 4),
         
         harm_future_gen = recode(harm_future_gen, "Not at all" = 1, "Only a little" = 2, "A moderate amount" = 3,
                                  "A great deal" = 4),
         
         harm_plants_animals = recode(harm_plants_animals, "Not at all" = 1, "Only a little" = 2, "A moderate amount" = 3,
                                      "A great deal" = 4),
         
         when_harm_US = recode(when_harm_US, "Never" = 1, "one_hun_years" = 2, "fifty_years" = 3, "twn_five_years" = 4,
                               "ten_years" = 5, "right_now" = 6))

heat_data <- heat_data %>% dplyr::select(worry, cause_recoded, happening, harm_personally, harm_US, harm_future_gen, harm_plants_animals,
                                         when_harm_US)
library(ggcorrplot)
library(reshape2)

Attaching package: 'reshape2'
The following object is masked from 'package:tidyr':

    smiths
variable_order <- c("worry", setdiff(names(heat_data), "worry"))

heat_data <- heat_data[, variable_order]

corr_mat <- round(cor(heat_data), 2)
Warning in cor(heat_data): the standard deviation is zero
p_mat <- cor_pmat(heat_data)
Warning in cor(x, y): the standard deviation is zero
Warning in cor(x, y): the standard deviation is zero

Warning in cor(x, y): the standard deviation is zero

Warning in cor(x, y): the standard deviation is zero

Warning in cor(x, y): the standard deviation is zero

Warning in cor(x, y): the standard deviation is zero

Warning in cor(x, y): the standard deviation is zero
corr_mat <- ggcorrplot(
  corr_mat, type = "lower",
  outline.color = "white",
  p.mat = p_mat,
  lab = F) +
  labs(title = "Correlation plot of all variables", x = "Variable 1", y = "Variable 2")
pl <- corr_mat + scale_fill_gradient2(limit = c(0.4,1), low = "blue", high =  "darkred", mid = "pink", midpoint = 0.75)
Scale for fill is already present.
Adding another scale for fill, which will replace the existing scale.
ggplotly(pl)
# ggplotly(corr_mat)

Tables

# testing my hypothesis with the ordinal logit regression 
fitData1 <- myData %>%
  mutate(worry = recode(worry, "1" = "Not at all worried", "2" = "Not very worried", "3" = "Somewhat worried", "4" = "Very worried"))

fitData1$worry <- as.factor(fitData1$worry)
# testing my hypothesis with the ordinal logit regression 
fitData2 <- sub_hap %>%
  mutate(worry = recode(worry, "1" = "Not at all worried", "2" = "Not very worried", "3" = "Somewhat worried", "4" = "Very worried"))

fitData2$worry <- as.factor(fitData2$worry)
# testing my hypothesis with the ordinal logit regression 
fitData3 <- sub_cause_hap %>%
  mutate(worry = recode(worry, "1" = "Not at all worried", "2" = "Not very worried", "3" = "Somewhat worried", "4" = "Very worried"))

fitData3$worry <- as.factor(fitData3$worry)

fitData3
# A tibble: 546 × 11
   wave    happening cause_recoded worry harm_personally harm_US harm_future_gen
   <chr>   <fct>     <fct>         <fct> <fct>           <fct>   <fct>          
 1 Oct 20… Yes       human_act     Very… A great deal    A grea… A great deal   
 2 Oct 20… Yes       human_act     Some… A great deal    A grea… A great deal   
 3 Oct 20… Yes       human_act     Very… A moderate amo… A mode… A moderate amo…
 4 Oct 20… Yes       human_act     Some… A moderate amo… A mode… A great deal   
 5 Oct 20… Yes       human_act     Some… A great deal    A grea… A great deal   
 6 Oct 20… Yes       human_act     Some… A great deal    A grea… A great deal   
 7 Oct 20… Yes       human_act     Some… A moderate amo… A mode… A great deal   
 8 Oct 20… Yes       human_act     Some… A great deal    A grea… A great deal   
 9 Oct 20… Yes       Human_act_an… Not … Not at all      Not at… A moderate amo…
10 Oct 20… Yes       human_act     Some… A great deal    A grea… A great deal   
# ℹ 536 more rows
# ℹ 4 more variables: harm_plants_animals <fct>, when_harm_US <fct>,
#   generation <fct>, education <fct>
# Ordinal logit model for all models

fitwM1 <- polr(worry ~ happening + cause_recoded + harm_personally + harm_US + harm_future_gen + harm_plants_animals +
                 when_harm_US + generation + education, data = fitData1, Hess = T)
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
fitwM2 <- polr(worry ~ happening + cause_recoded + harm_personally + harm_US + harm_future_gen + harm_plants_animals +
                 when_harm_US + generation + education, data = fitData2, Hess = T)
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning in polr(worry ~ happening + cause_recoded + harm_personally + harm_US +
: design appears to be rank-deficient, so dropping some coefs
fitwM3 <- polr(worry ~ happening + cause_recoded + harm_personally + harm_US + harm_future_gen + harm_plants_animals +
                 when_harm_US + generation + education, data = fitData3, Hess = T)
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
Warning in polr(worry ~ happening + cause_recoded + harm_personally + harm_US +
: design appears to be rank-deficient, so dropping some coefs
#summary(fitwM1)



myModels <- list(fitwM1, fitwM2, fitwM3)

stargazer(myModels, align = TRUE, type = "text", report = ('vc*p'), column.sep.width = "-1pt",  no.space = TRUE,
          title = "Ordered Logistic Regression Results", column.labels = c("Model 1","Happening",
                                                                           "Happening and Human Involvemnt"),
          omit = c("cause_recodedCC_not_happening", "generation", "education"))

Ordered Logistic Regression Results
===========================================================================================
                                                      Dependent variable:                  
                                     ------------------------------------------------------
                                                             worry                         
                                       Model 1    Happening  Happening and Human Involvemnt
                                         (1)         (2)                  (3)              
-------------------------------------------------------------------------------------------
happeningYes                            0.379                                              
                                      p = 0.297                                            
cause_recodedNat_changes              -0.858**    -0.957**                                 
                                      p = 0.024   p = 0.016                                
cause_recodedhuman_act                  0.139       0.252                0.377             
                                      p = 0.686   p = 0.476            p = 0.301           
harm_personallyOnly a little          1.395***    1.573***              2.328***           
                                     p = 0.0004   p = 0.001            p = 0.001           
harm_personallyA moderate amount      2.049***    2.174***              2.769***           
                                     p = 0.00001 p = 0.00002           p = 0.0001          
harm_personallyA great deal           2.595***    2.731***              3.306***           
                                     p = 0.00000 p = 0.00000          p = 0.00001          
harm_USOnly a little                   -0.048      -0.237                -1.435            
                                      p = 0.920   p = 0.718            p = 0.195           
harm_USA moderate amount                0.292       0.219                -0.775            
                                      p = 0.600   p = 0.760            p = 0.488           
harm_USA great deal                    1.502**     1.449*                0.315             
                                      p = 0.012   p = 0.053            p = 0.782           
harm_future_genOnly a little           0.982*      -0.429                0.718             
                                      p = 0.071   p = 0.662            p = 0.696           
harm_future_genA moderate amount       1.576**      0.581                1.408             
                                      p = 0.019   p = 0.589            p = 0.489           
harm_future_genA great deal           2.160***      1.034                1.916             
                                      p = 0.003   p = 0.347            p = 0.345           
harm_plants_animalsOnly a little        0.509       1.498                3.409*            
                                      p = 0.341   p = 0.157            p = 0.100           
harm_plants_animalsA moderate amount   1.107*      1.926*                2.810             
                                      p = 0.100   p = 0.092            p = 0.196           
harm_plants_animalsA great deal        1.708**     2.565**               3.574             
                                      p = 0.017   p = 0.029            p = 0.101           
when_harm_USone_hun_years               0.416      1.515**              3.777**            
                                      p = 0.348   p = 0.035            p = 0.026           
when_harm_USfifty_years                -0.080       0.381                2.440             
                                      p = 0.868   p = 0.604            p = 0.154           
when_harm_UStwn_five_years              0.288       0.812                2.998*            
                                      p = 0.572   p = 0.264            p = 0.078           
when_harm_USten_years                  1.303**     1.887**              4.341**            
                                      p = 0.014   p = 0.011            p = 0.012           
when_harm_USright_now                 2.024***    2.506***              5.005***           
                                     p = 0.00005 p = 0.0005            p = 0.004           
-------------------------------------------------------------------------------------------
Observations                             818         678                  546              
===========================================================================================
Note:                                                           *p<0.1; **p<0.05; ***p<0.01
m3 <- broom::tidy(fitwM3) %>% filter(term %in% c("harm_personallyA great deal", 
                                                "harm_personallyA moderate amount", "harm_personallyOnly a little",
                                                
                                                "harm_USOnly a little", "harm_USA moderate amount",
                                                "harm_USA great deal",
                                                
                                                "harm_future_genOnly a little", "harm_future_genA moderate amount",
                                                "harm_future_genA great deal", 
                                                
                                                "harm_plants_animalsOnly a little",
                                                "harm_plants_animalsA moderate amount", "harm_plants_animalsA great deal",
                                                
                                                "when_harm_USone_hun_years", "when_harm_USfifty_years",
                                                "when_harm_UStwn_five_years", "when_harm_USten_years",
                                                "when_harm_USright_now")) %>%
  mutate(model = "Happening and Human Involvemnt")

m3 <- m3 %>%
  mutate(p_value = 2 * (1 - pnorm(abs(estimate / std.error))),
    significance = case_when(
      p_value < 0.01 ~ "***",
      p_value < 0.05 ~ "**",
      p_value < 0.1 ~ "*",
      TRUE ~ ""))  # "" for not significant
dwplot(m3,
       vline = geom_vline(
           xintercept = 0,
           colour = "grey60",
           linetype = 2),
       vars_order = c("harm_personallyA great deal", "harm_personallyA moderate amount", "harm_personallyOnly a little",
                      
                      "harm_USA great deal", "harm_USA moderate amount", "harm_USOnly a little",
                      
                      "harm_future_genA great deal", "harm_future_genA moderate amount", "harm_future_genOnly a little",
                      
                      "harm_plants_animalsA great deal", "harm_plants_animalsA moderate amount",
                      "harm_plants_animalsOnly a little",
                      
                      "when_harm_USright_now", "when_harm_USten_years", "when_harm_UStwn_five_years",
                      "when_harm_USfifty_years", "when_harm_USone_hun_years")) %>% 
  relabel_predictors( 
    c("harm_personallyA great deal" = "harm personally a great deal",
      "harm_personallyA moderate amount" = "harm personally a moderate amount",
      "harm_personallyOnly a little" = "harm personally only a little",
      
      "harm_USA great deal" = "harm the US a great deal",
      "harm_USA moderate amount" = "harm the US a moderate amount", 
      "harm_USOnly a little" = "harm the US only a little",
      
      "harm_future_genA great deal" = "harm future generations a great deal", 
      "harm_future_genA moderate amount" = "harm future generations a moderate amount",
      "harm_future_genOnly a little" = "harm future generations only a little",
      
      "harm_plants_animalsA great deal" = "harm plants & animals a great deal", 
      "harm_plants_animalsA moderate amount" = "harm plants & animals a moderate amount",
      "harm_plants_animalsOnly a little" = "harm plants & animals only a little",
      
      "when_harm_USright_now" = "harming the US right now", 
      "when_harm_USten_years" = "will harm the US in 10 years", 
      "when_harm_UStwn_five_years" = "will harm the US in 25 years",
      "when_harm_USfifty_years" = "will harm the US in 50 years",
      "when_harm_USone_hun_years" = "will harm the US in 100 years")
  ) +
  geom_text(
    aes(label = significance, x = estimate, y = term),
    position = position_dodge(width = 0.8),
    size = 4, color = "red", hjust = -0.2
  ) +
  xlab("Coefficients Estimate") +
  ylab("") +
  ggtitle("Predicting Worry") +
  theme_bw(base_size = 10) +
    scale_colour_grey(name = "Models")

Interpreting my Model (Model #3)

# making a nice table for my model

modelTable <- coef(summary(fitwM3))

pValue <- pnorm(abs(modelTable[, "t value"]), lower.tail = F) * 2

modelTable <- cbind(modelTable, "p value" = pValue)

stargazer(modelTable, type = "text", title = "My Final Model: Model #3 Table")

My Final Model: Model #3 Table
======================================================================
                                     Value  Std. Error t value p value
----------------------------------------------------------------------
cause_recodedhuman_act               0.377    0.364     1.036   0.300 
harm_personallyOnly a little         2.328    0.696     3.343   0.001 
harm_personallyA moderate amount     2.769    0.710     3.900  0.0001 
harm_personallyA great deal          3.306    0.747     4.427  0.00001
harm_USOnly a little                 -1.435   1.105    -1.298   0.194 
harm_USA moderate amount             -0.775   1.116    -0.694   0.487 
harm_USA great deal                  0.315    1.135     0.277   0.781 
harm_future_genOnly a little         0.718    1.834     0.391   0.695 
harm_future_genA moderate amount     1.408    2.032     0.693   0.488 
harm_future_genA great deal          1.916    2.028     0.945   0.345 
harm_plants_animalsOnly a little     3.409    2.067     1.649   0.099 
harm_plants_animalsA moderate amount 2.810    2.172     1.294   0.196 
harm_plants_animalsA great deal      3.574    2.179     1.640   0.101 
when_harm_USone_hun_years            3.777    1.692     2.232   0.026 
when_harm_USfifty_years              2.440    1.711     1.426   0.154 
when_harm_UStwn_five_years           2.998    1.698     1.765   0.078 
when_harm_USten_years                4.341    1.712     2.535   0.011 
when_harm_USright_now                5.005    1.694     2.955   0.003 
generationMillennials                0.389    0.373     1.043   0.297 
generationGenerationX                0.281    0.375     0.748   0.454 
generationBabyBoomers                0.231    0.365     0.632   0.527 
generationSilent                     -0.277   0.495    -0.560   0.575 
educationhighSchool                  0.500    0.527     0.948   0.343 
educationsomeCollege                 0.581    0.523     1.112   0.266 
educationbachelorDegreeUp            0.524    0.502     1.043   0.297 
Not at all worried| Not very worried 4.406    2.033     2.168   0.030 
Not very worried| Somewhat worried   9.259    2.589     3.577  0.0003 
Somewhat worried| Very worried       13.586   2.620     5.185  0.00000
----------------------------------------------------------------------

Probability of Being Worried

probability <- predict(fitwM3, type = "probs")

#head(probability)

fitData3$prob <- probability
prob_personal <- fitData3 %>% 
  group_by(harm_personally, worry) %>% 
  summarise(prob_nw = mean(prob[,1]),
            prob_nvw = mean(prob[,2]),
            prob_sw = mean(prob[,3]),
            prob_vw = mean(prob[,4]))
`summarise()` has grouped output by 'harm_personally'. You can override using
the `.groups` argument.
prob_personal
# A tibble: 13 × 6
# Groups:   harm_personally [4]
   harm_personally   worry                prob_nw prob_nvw prob_sw  prob_vw
   <fct>             <fct>                  <dbl>    <dbl>   <dbl>    <dbl>
 1 Not at all        Not at all worried 0.574      0.415    0.0109 0.000149
 2 Not at all        Not very worried   0.101      0.614    0.277  0.00760 
 3 Not at all        Somewhat worried   0.00758    0.407    0.556  0.0290  
 4 Not at all        Very worried       0.000504   0.0602   0.770  0.170   
 5 Only a little     Not very worried   0.0195     0.380    0.535  0.0658  
 6 Only a little     Somewhat worried   0.00141    0.115    0.656  0.228   
 7 Only a little     Very worried       0.000167   0.0206   0.529  0.450   
 8 A moderate amount Not very worried   0.000897   0.0889   0.638  0.272   
 9 A moderate amount Somewhat worried   0.000400   0.0414   0.525  0.434   
10 A moderate amount Very worried       0.000154   0.0154   0.366  0.619   
11 A great deal      Not very worried   0.00204    0.179    0.665  0.155   
12 A great deal      Somewhat worried   0.0000952  0.0116   0.309  0.679   
13 A great deal      Very worried       0.0000268  0.00338  0.185  0.812   
prob_personal_long <- prob_personal %>%
  pivot_longer(cols = starts_with("prob"),
               names_to = "Worry",
               values_to = "Probability") %>% 
  mutate(Probability = round(Probability * 100, 1))

prob_personal_long$Worry <- factor(prob_personal_long$Worry,
                                   levels = c("prob_nw", "prob_nvw", "prob_sw", "prob_vw"),
                                   labels = c("Not at all worried", "Not very worried", "Somewhat worried", "Very worried"))

prob_personal_long$harm_personally <- factor(prob_personal_long$harm_personally,
                                   labels = c("Not at all", "Only a little", "A moderate amount", "A great deal"))

prob_personal_long
# A tibble: 52 × 4
# Groups:   harm_personally [4]
   harm_personally worry              Worry              Probability
   <fct>           <fct>              <fct>                    <dbl>
 1 Not at all      Not at all worried Not at all worried        57.4
 2 Not at all      Not at all worried Not very worried          41.5
 3 Not at all      Not at all worried Somewhat worried           1.1
 4 Not at all      Not at all worried Very worried               0  
 5 Not at all      Not very worried   Not at all worried        10.1
 6 Not at all      Not very worried   Not very worried          61.4
 7 Not at all      Not very worried   Somewhat worried          27.7
 8 Not at all      Not very worried   Very worried               0.8
 9 Not at all      Somewhat worried   Not at all worried         0.8
10 Not at all      Somewhat worried   Not very worried          40.7
# ℹ 42 more rows
prob_per <- prob_personal_long[prob_personal_long$worry==prob_personal_long$Worry, ]

prob_per
# A tibble: 13 × 4
# Groups:   harm_personally [4]
   harm_personally   worry              Worry              Probability
   <fct>             <fct>              <fct>                    <dbl>
 1 Not at all        Not at all worried Not at all worried        57.4
 2 Not at all        Not very worried   Not very worried          61.4
 3 Not at all        Somewhat worried   Somewhat worried          55.6
 4 Not at all        Very worried       Very worried              17  
 5 Only a little     Not very worried   Not very worried          38  
 6 Only a little     Somewhat worried   Somewhat worried          65.6
 7 Only a little     Very worried       Very worried              45  
 8 A moderate amount Not very worried   Not very worried           8.9
 9 A moderate amount Somewhat worried   Somewhat worried          52.5
10 A moderate amount Very worried       Very worried              61.9
11 A great deal      Not very worried   Not very worried          17.9
12 A great deal      Somewhat worried   Somewhat worried          30.9
13 A great deal      Very worried       Very worried              81.2
p_harm_plot <- plot_ly(prob_per, x = ~harm_personally, y = ~Probability, color = ~Worry, type = "bar",
                       barmode = "group", 
                       text = ~ paste(Probability, "%"), 
                       hovertext = ~paste("Worry Level:", Worry, "\n",
                                          "Potential Harm Level:", harm_personally, "\n", 
                                          "Percentage = ", Probability, "%"),
                       hoverinfo = "text",
                       colors = c("#D4B9DA", "#DF65B0", "#DD1C77", "#980043")) %>% 
  layout(title = "Probability of Worrying about Climate Change by Personal Harm", 
         xaxis = list(title = "Harm Level (Personal)"),
         yaxis = list(title = "Probability"), 
         legend = list(title=list(text='Worry Levels')))

p_harm_plot
Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
prob_when <- fitData3 %>% 
  group_by(when_harm_US, worry) %>% 
  summarise(prob_nw = mean(prob[,1]),
            prob_nvw = mean(prob[,2]),
            prob_sw = mean(prob[,3]),
            prob_vw = mean(prob[,4]))
`summarise()` has grouped output by 'when_harm_US'. You can override using the
`.groups` argument.
#prob_when

prob_when_long <- prob_when %>%
  pivot_longer(cols = starts_with("prob"),
               names_to = "Worry",
               values_to = "Probability") %>% 
  mutate(Probability = round(Probability * 100, 1))

prob_when_long$Worry <- factor(prob_when_long$Worry,
                                   levels = c("prob_nw", "prob_nvw", "prob_sw", "prob_vw"),
                                   labels = c("Not at all worried", "Not very worried", "Somewhat worried", "Very worried"))

prob_when_long$when_harm_US <- factor(prob_when_long$when_harm_US,
                                   labels = c("Never", "100 years", "50 years", "25 years", "10 years", "Right Now"))

#prob_when_long
prob_wh <- prob_when_long[prob_when_long$worry==prob_when_long$Worry, ]

prob_wh
# A tibble: 19 × 4
# Groups:   when_harm_US [6]
   when_harm_US worry              Worry              Probability
   <fct>        <fct>              <fct>                    <dbl>
 1 Never        Not at all worried Not at all worried        68.3
 2 Never        Not very worried   Not very worried          71.9
 3 100 years    Not very worried   Not very worried          54.7
 4 100 years    Somewhat worried   Somewhat worried          65.5
 5 100 years    Very worried       Very worried              45.3
 6 50 years     Not at all worried Not at all worried        19.5
 7 50 years     Not very worried   Not very worried          43.6
 8 50 years     Somewhat worried   Somewhat worried          65.7
 9 50 years     Very worried       Very worried              17.4
10 25 years     Not at all worried Not at all worried        73.6
11 25 years     Not very worried   Not very worried          39.3
12 25 years     Somewhat worried   Somewhat worried          71.6
13 25 years     Very worried       Very worried              21.6
14 10 years     Not very worried   Not very worried          26  
15 10 years     Somewhat worried   Somewhat worried          67.8
16 10 years     Very worried       Very worried              50.8
17 Right Now    Not very worried   Not very worried          10.5
18 Right Now    Somewhat worried   Somewhat worried          46.7
19 Right Now    Very worried       Very worried              69.3
p_when_plot <- plot_ly(prob_wh, x = ~when_harm_US, y = ~Probability, color = ~Worry, type = "bar",
                       barmode = "group", 
                       text = ~ paste(Probability, "%"), 
                       hovertext = ~paste("Worry Level:", Worry, "\n",
                                          "Potential Harm Level:", when_harm_US, "\n", 
                                          "Percentage = ", Probability, "%"),
                       hoverinfo = "text",
                       colors = c("#FDD49E", "#FC8D59", "#E34A33", "#B30000")) %>% 
  layout(title = "Probability of Worry by the Belief of when Climate Change will Harm", 
         xaxis = list(title = "Harm Level (Personal)"),
         yaxis = list(title = "Probability of Worry (%)"), 
         legend = list(title=list(text='Worry Levels')))

p_when_plot
Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

Warning: 'bar' objects don't have these attributes: 'barmode'
Valid attributes include:
'_deprecated', 'alignmentgroup', 'base', 'basesrc', 'cliponaxis', 'constraintext', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'insidetextanchor', 'insidetextfont', 'legendgroup', 'legendgrouptitle', 'legendrank', 'marker', 'meta', 'metasrc', 'name', 'offset', 'offsetgroup', 'offsetsrc', 'opacity', 'orientation', 'outsidetextfont', 'selected', 'selectedpoints', 'showlegend', 'stream', 'text', 'textangle', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'width', 'widthsrc', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'